#include "mode_cleaning.h"
#include "main_motor.h"
#include "actuator_control.h"
#include "timer_utils.h"

// 清洗模式内部状态
static struct {
    uint8_t current_phase;
    bool phase_complete[3];
} s_clean_state;

// 清洗模式进入
void cleaning_mode_enter(ModeConfig* config) {
    memset(&s_clean_state, 0, sizeof(s_clean_state));
    
    // 初始化电机配置
    MotorConfig motor_cfg = {
        .min_current = 100,    // 清洗时电流阈值放宽
        .max_current = 1000,
        .start_timeout = 5000,
        .retry_count = 3,
        .protection_time = 30000
    };
    motor_init(&motor_cfg);
    
    s_clean_state.current_phase = 1;
    
    printf("Entering cleaning mode\n");
}

// 清洗模式处理
void cleaning_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state) {
    uint32_t elapsed = state->mode_runtime;
    uint32_t phase1_end = config->cleaning_phase1_time;
    uint32_t phase2_end = phase1_end + config->cleaning_phase2_time;
    uint32_t total_time = phase2_end + config->cleaning_phase3_time;
    
    // 处理电机状态
    motor_process(sensors->motor_current);
    state->motor_running = (motor_get_state() == MOTOR_STATE_RUNNING);
    
    // 检查电机错误
    if (motor_get_error() == MOTOR_ERROR_START_FAILED) {
        state->in_error = true;
        state->error_code = ERROR_MOTOR_CURRENT;
        return;
    }
    
    // 清洗完成
    if (elapsed >= total_time) {
        state->comp_running = false;
        state->bypass_valve = false;
        state->cooling_valve = false;
        state->motor_running = false;
        state->pump_running = false;
        state->mixer_running = false;
        state->sterilize_on = false;
        
        motor_stop();
        actuator_set_compressor(false);
        actuator_set_bypass_valve(false);
        actuator_set_sterilize_light(false);
        return;
    }
    
    // 阶段1: 加热+循环清洗
    if (elapsed < phase1_end) {
        s_clean_state.current_phase = 1;
        if (!s_clean_state.phase_complete[0]) {
            // 开启加热（通过旁通阀实现）
            state->bypass_valve = true;
            state->comp_running = true;
            actuator_set_bypass_valve(true);
            actuator_set_compressor(true);
            
            // 启动电机和搅拌机
            if (motor_get_state() == MOTOR_STATE_STOPPED) {
                motor_start();
            }
            actuator_set_mixer(true);
            state->mixer_running = true;
            
            // 达到目标温度标记阶段完成
            if (sensors->cabinet_temp >= config->cleaning_target_temp) {
                s_clean_state.phase_complete[0] = true;
            }
        }
    }
    // 阶段2: 循环清洗
    else if (elapsed < phase2_end) {
        s_clean_state.current_phase = 2;
        if (!s_clean_state.phase_complete[1]) {
            // 关闭加热
            state->bypass_valve = false;
            state->comp_running = false;
            actuator_set_bypass_valve(false);
            actuator_set_compressor(false);
            
            // 保持电机和搅拌机运行
            if (motor_get_state() == MOTOR_STATE_STOPPED) {
                motor_start();
            }
            actuator_set_mixer(true);
            state->mixer_running = true;
            
            s_clean_state.phase_complete[1] = true;
        }
    }
    // 阶段3: 杀菌
    else {
        s_clean_state.current_phase = 3;
        if (!s_clean_state.phase_complete[2]) {
            // 停止电机，开启杀菌灯
            motor_stop();
            actuator_set_mixer(false);
            state->mixer_running = false;
            actuator_set_sterilize_light(true);
            state->sterilize_on = true;
            
            s_clean_state.phase_complete[2] = true;
        }
    }
    
    // 膨化泵与电机同步
    state->pump_running = state->motor_running;
    if (state->motor_running && !state->pump_running) {
        actuator_set_pump(true);
        state->pump_running = true;
    } else if (!state->motor_running && state->pump_running) {
        actuator_set_pump(false);
        state->pump_running = false;
    }
}

// 清洗模式退出
void cleaning_mode_exit(void) {
    motor_stop();
    actuator_set_compressor(false);
    actuator_set_bypass_valve(false);
    actuator_set_pump(false);
    actuator_set_mixer(false);
    actuator_set_sterilize_light(false);
    
    printf("Exiting cleaning mode\n");
}
